import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_acrylic/flutter_acrylic.dart' as flutter_acrylic;
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

import '../../config.dart';
import '../../constants.dart';
import '../../route/router.dart';
import 'app_theme.dart';
import 'app_view_model.dart';

///应用View入口
class AppViewMain extends StatefulWidget {
  const AppViewMain({super.key});

  @override
  State<AppViewMain> createState() => _AppViewMainState();
}

class _AppViewMainState extends State<AppViewMain> {
  @override
  Widget build(final BuildContext context) {
    //初始化ScreenUtil
    ScreenUtil.init(context, designSize: constants.designSize);
    //监听处理
    return Consumer2<AppTheme, AppViewModel>(
      builder: (ctx, appTheme, _, __) => buildRouter(ctx, appTheme),
    );
  }

  Widget buildRouter(final BuildContext ctx, final AppTheme appTheme) =>
      FluentApp.router(
        title: appTitle,
        themeMode: appTheme.mode,
        debugShowCheckedModeBanner: false,
        color: appTheme.color,
        darkTheme: FluentThemeData(
          brightness: Brightness.dark,
          accentColor: appTheme.color,
          visualDensity: VisualDensity.standard,
          focusTheme: FocusThemeData(
            glowFactor: is10footScreen(context) ? 2.0 : 0.0,
          ),
        ),
        theme: FluentThemeData(
          accentColor: appTheme.color,
          visualDensity: VisualDensity.standard,
          focusTheme: FocusThemeData(
            glowFactor: is10footScreen(context) ? 2.0 : 0.0,
          ),
        ),
        supportedLocales: const [
          Locale("zh", "CN"),
          Locale("en", "US"),
        ],
        locale: appTheme.locale,
        builder: EasyLoading.init(
          builder: (_, child) => buildDirectionality(appTheme, child),
        ),
        routeInformationParser: router.routeInformationParser,
        routerDelegate: router.routerDelegate,
        routeInformationProvider: router.routeInformationProvider,
      );

  Widget buildDirectionality(final AppTheme appTheme, final Widget? child) {
    return Directionality(
      textDirection: appTheme.textDirection,
      child: NavigationPaneTheme(
        data: NavigationPaneThemeData(
          backgroundColor:
              appTheme.windowEffect != flutter_acrylic.WindowEffect.disabled
                  ? Colors.transparent
                  : null,
        ),
        child: child!,
      ),
    );
  }
}
